//
//  SISSCommonContractViewModel.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/9/22.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSCommonContractViewModel.h"
#import "SISSContractRightFillInCell.h"
#import "SISSContractRightFillinUnitCell.h"
#import "SISSContractBottomFillInCell.h"
#import "SISSContractChooseCell.h"
#import "SISSContractFillInPropertyCell.h"
#import "SISSContractOnlyRightFillInCell.h"
#import "SISSContractFillInPlatTypeHouseCell.h"
#import "SISSContractCheckCell.h"
#import "SISSContractAlternativeCell.h"
#import "UIViewController+CurrentViewController.h"
#import "SISSContractRightShowCell.h"

@interface SISSCommonContractViewModel ()<SISSContractRightFillinUnitCellDelegate>

@end
@implementation SISSCommonContractViewModel

- (instancetype)init {
    if (self = [super init]) {
        
        if ([self plistName].length > 0) {
            //获取plist
            NSURL *url = [[NSBundle mainBundle] URLForResource:[self plistName] withExtension:@"plist"];
            
            //根据plist加载数组
            NSArray *arr = [NSArray arrayWithContentsOfURL:url];
            NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:arr.count];
            
            [arr enumerateObjectsUsingBlock:^(NSArray *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                [arrM addObject: [NSArray yy_modelArrayWithClass:[SISSContractModel class] json:obj]];
            }];
            self.dataArr = arrM;
        }
    }
    return self;
}

- (void)registContractClass:(UITableView *)tableView {
    
    self.tableView = tableView;
    [tableView registerClass:[SISSContractRightFillInCell class] forCellReuseIdentifier:contractRightFillInCellReuseID];
    [tableView registerClass:[SISSContractOnlyRightFillInCell class] forCellReuseIdentifier:contractOnlyRightFillInCellReuseID];
    [tableView registerClass:[SISSContractRightFillinUnitCell class] forCellReuseIdentifier:contractRightFillInUnitCellReuseID];
    [tableView registerClass:[SISSContractBottomFillInCell class] forCellReuseIdentifier:contractBottomFillInCellReuseID];
    [tableView registerClass:[SISSContractChooseCell class] forCellReuseIdentifier:contractChooseCellReuseID];
    [tableView registerClass:[SISSContractCheckCell class] forCellReuseIdentifier:contractCheckeCellReuseID];
    [tableView registerClass:[SISSContractAlternativeCell class] forCellReuseIdentifier:contractAlternativeCellReuseID];
    [tableView registerClass:[SISSContractFillInPlatTypeHouseCell class] forCellReuseIdentifier:contractFillInPlatTypeHouseCellReuseID];
    [tableView registerClass:[SISSContractFillInPropertyCell class] forCellReuseIdentifier:contractFillInPropertyCellReuseID];
    [tableView registerClass:[SISSContractRightShowCell class] forCellReuseIdentifier:contractRightShowCellReuseID];
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:contractNormalCellReuseID];
}

- (NSUInteger)getSectionCount {
    return self.dataArr.count;
}

- (NSUInteger)getRowsInSection:(NSInteger)section {
    return self.dataArr[section].count;
}

- (UITableViewCell *)getCell:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
    
    NSInteger type = self.dataArr[indexPath.section][indexPath.row].cellType;
    if (type == 1) {
        SISSContractRightFillInCell *cell = [tableView dequeueReusableCellWithIdentifier:contractRightFillInCellReuseID forIndexPath:indexPath];
        cell.contractModel = self.dataArr[indexPath.section][indexPath.row];
        return cell;
    } else if (type == 2) {
        SISSContractBottomFillInCell *bottomCell = [tableView dequeueReusableCellWithIdentifier:contractBottomFillInCellReuseID forIndexPath:indexPath];
        bottomCell.contractModel = self.dataArr[indexPath.section][indexPath.row];
        return bottomCell;
        
    } else if (type == 3) {
        SISSContractChooseCell *chooseCell = [tableView dequeueReusableCellWithIdentifier:contractChooseCellReuseID forIndexPath:indexPath];
        chooseCell.delegate = self;
        chooseCell.contractModel = self.dataArr[indexPath.section][indexPath.row];
        return chooseCell;
    } else if (type == 4) {
        SISSContractRightFillinUnitCell *unitCell = [tableView dequeueReusableCellWithIdentifier:contractRightFillInUnitCellReuseID forIndexPath:indexPath];
        if (self.dataArr[indexPath.section][indexPath.row].isChecked) {
            unitCell.delegate = self;
        }
        unitCell.contractModel = self.dataArr[indexPath.section][indexPath.row];
        return unitCell;
    } else if (type == 5) {
        SISSContractCheckCell *checkCell = [tableView dequeueReusableCellWithIdentifier:contractCheckeCellReuseID forIndexPath:indexPath];
        checkCell.contractModel = self.dataArr[indexPath.section][indexPath.row];
        return checkCell;
    } else if (type == 6) {
        SISSContractAlternativeCell *alternativeCell = [tableView dequeueReusableCellWithIdentifier:contractAlternativeCellReuseID forIndexPath:indexPath];
        alternativeCell.contractModel = self.dataArr[indexPath.section][indexPath.row];
        return alternativeCell;
    } else if (type == 7) {
        SISSContractFillInPropertyCell *propertyCell = [tableView dequeueReusableCellWithIdentifier:contractFillInPropertyCellReuseID forIndexPath:indexPath];
        propertyCell.contractModel = self.dataArr[indexPath.section][indexPath.row];
        return propertyCell;
    } else if (type == 8) {
        SISSContractOnlyRightFillInCell *cell = [tableView dequeueReusableCellWithIdentifier:contractOnlyRightFillInCellReuseID forIndexPath:indexPath];
        cell.contractModel = self.dataArr[indexPath.section][indexPath.row];
        return cell;
    } else if (type == 99) {
        SISSContractRightShowCell *cell = [tableView dequeueReusableCellWithIdentifier:contractRightShowCellReuseID forIndexPath:indexPath];
        cell.contractModel = self.dataArr[indexPath.section][indexPath.row];
        return cell;
    } else {
        UITableViewCell *normalCell = [tableView dequeueReusableCellWithIdentifier:contractNormalCellReuseID forIndexPath:indexPath];
        [normalCell.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(normalCell.contentView).offset(8);
            make.bottom.right.equalTo(normalCell.contentView).offset(-8);
        }];
        normalCell.textLabel.textColor = MAIN_TEXT_COLOR;
        normalCell.textLabel.numberOfLines = 0;
        normalCell.textLabel.font = [UIFont systemFontOfSize:16];
        normalCell.textLabel.text = self.dataArr[indexPath.section][indexPath.row].leftStr;
        UIView *bottomLine = [[UIView alloc] init];
        bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [normalCell.contentView addSubview:bottomLine];
        [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
            //        make.top.equalTo(weakSelf.textView.mas_bottom).offset(1.5 * margin);
            make.left.right.bottom.equalTo(normalCell.contentView);
            make.height.mas_equalTo(1);
        }];
        return normalCell;
    }
}

- (UIView *)getViewHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)getHeightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

#pragma mark -- SISSContractRightFillinUnitCellDelegate
- (void)contractRightFillinUnitCellTextFieldDidEndEditing {
    [self.tableView reloadData];
}

#pragma mark -- SISSContractChooseCellDelegate 选择后的回调
- (void)contractChooseCell:(SISSContractChooseCell *)cell chooseIndex:(NSInteger)index {
    
}

- (NSString *)plistName {
    return @"";
}

@end
